package com.lagou.StageOne.Module4.Assignment.Assignment1;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 1. 编程题
 *
 *     a.使用 List 集合实现简易的学生信息管理系统，要求打印字符界面提示用户选择相应的功 能，根据用户输入的选择去实现增加、删除、修改、查找以及遍历所有学生信息的功能。
 *
 *    b.自定义学号异常类和年龄异常类，并在该成员变量不合理时产生异常对象并抛出。
 *
 *    c.当系统退出时将 List 集合中所有学生信息写入到文件中，当系统启动时读取文件中所 有学生信息到 List 集合中。
 */
public class Test {

    List<Student> studentList = new ArrayList<>();

    public static void main(String[] args) throws StudentAgeException, StudentIdException {
        boolean boo = true;
        Test t = new Test();
        System.out.println("欢迎使用学生管理系统！！！！！");
        System.out.println("正在加载学生信息，请稍等----");
        ObjectInputStream objectInputStream = null;
        File file = new File("d:/student.txt");
        //
        if (file.isFile()) {


            try {
                objectInputStream = new ObjectInputStream(new FileInputStream("d:/student.txt"));
                t.studentList = (List<Student>) objectInputStream.readObject();
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        while (boo){
            System.out.println("请选择要进行的操作:");
            System.out.println("1.增加学生信息");
            System.out.println("2.删除学生信息");
            System.out.println("3.修改学生信息");
            System.out.println("4.查询学生信息");
            System.out.println("5.查询全部学生信息");
            System.out.println("0.退出");
            System.out.println("请输入:");
            Scanner scanner = new Scanner(System.in);
            Integer i = scanner.nextInt();
            if (null!=i){
                if (i==1){
                    System.out.println("请输入添加学生的信息:");
                    System.out.println("姓名:");
                    String name = scanner.next();
                    System.out.println("年龄:");
                    Integer age = scanner.nextInt();
                    System.out.println("性别:");
                    String sex =scanner.next();
                    System.out.println("学号:");
                    Integer studentId =scanner.nextInt();
                    Student student = new Student();
                    student.setSex(sex);
                    student.setName(name);
                    student.setStudentId(studentId);
                    student.setAge(age);
                    try {
                        t.insertPerson(student);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else if (i==2){
                    System.out.println("请输入要删除学生的信息:");
                    System.out.println("学号:");
                    Integer studentId = scanner.nextInt();
                    Student student = new Student();
                    student.setStudentId(studentId);
                    try {
                        t.deletePersonById(studentId);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else if (i==3){
                    System.out.println("请输入要修改学生的信息:");
                    System.out.println("学号:");
                    Integer studentId = scanner.nextInt();
                    System.out.println("姓名:");
                    String name = scanner.next();
                    System.out.println("年龄:");
                    Integer age = scanner.nextInt();
                    System.out.println("性别:");
                    String sex =scanner.next();
                    Student student = new Student();
                    student.setSex(sex);
                    student.setName(name);
                    student.setStudentId(studentId);
                    student.setAge(age);
                    try {
                        t.updatePerson(student);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else if (i==4){
                    System.out.println("请输入要查询学生的信息:");
                    System.out.println("学号:");
                    Integer studentId = scanner.nextInt();
                    try {
                        t.selectStudentById(studentId);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else if (i==5){
                    try {
                        t.selectStudentAll();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else if (i==0){
                     file = new File("d:/student.txt");
                    if (!file.isFile()){
                        try {
                            file.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    ObjectOutputStream objectOutputStream = null;
                        try {
                            objectOutputStream = new ObjectOutputStream(new FileOutputStream("d:/student.txt"));
                            objectOutputStream.writeObject(t.studentList);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }finally {
                            try {
                                objectOutputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    boo = false;
                }
            }else{
                System.out.println("请重新输入:");
                boo=true;
            }
        }
    }

    /**
     * 添加学生信息
     * @param student
     */
    public void insertPerson(Student student) throws InterruptedException {
        if (null != student) {
            studentList.add(student);
                System.out.println("正在添加，请稍等-----");
                Thread.sleep(1000);
                System.out.println("添加成功");
                System.out.println("正在返回主菜单-----");
                Thread.sleep(1000);
        }
    }
    /**
     * 根据学号删除学生信息
     * @param studentId
     */
    public void deletePersonById(Integer studentId) throws InterruptedException {
        if (studentId>0){
            for (Student student1 : studentList) {
                if (student1.getStudentId().equals(studentId)){
                    studentList.remove(student1);
                    System.out.println("正在删除，请稍等-----");
                    Thread.sleep(1000);
                    System.out.println("删除成功");
                    System.out.println("正在返回主菜单-----");
                    Thread.sleep(1000);
                    return;
                }
            }
                System.out.println("该学生信息不存在");
                System.out.println("正在返回主菜单-----");
                Thread.sleep(1000);
            }
        }


    /**
     * 修改学生信息
     * @param student
     */
    public void updatePerson(Student student) throws InterruptedException, StudentAgeException, StudentIdException {
        if (null != student){
            for (Student student1 : studentList) {
                if (student1.getStudentId().equals(student.getStudentId())){
                    student1.setStudentId(student.getStudentId());
                    student1.setName(student.getName());
                    student1.setAge(student.getAge());
                    student1.setSex(student.getSex());
                        System.out.println("正在修改,请稍等-----");
                        Thread.sleep(1000);
                        System.out.println("修改成功");
                        System.out.println("正在返回主菜单-----");
                        Thread.sleep(1000);
                }else{
                    System.out.println("该学生信息不存在");
                    System.out.println("正在返回主菜单-----");
                    Thread.sleep(1000);
                }
            }
        }
    }

    public void selectStudentById(Integer studentId) throws InterruptedException {
        if (null != studentId ){
            for (Student student1 : studentList) {
                if (student1.getStudentId().equals(studentId)){
                    System.out.println("正在查询，请稍后------");
                    Thread.sleep(1000);
                    System.out.println("该学生的信息为:"+student1);
                    System.out.println("正在返回主菜单-----");
                    Thread.sleep(1000);
                }else{
                    System.out.println("该学生信息不存在");
                    System.out.println("正在返回主菜单-----");
                    Thread.sleep(1000);
                }
            }
        }
    }

    public void selectStudentAll() throws InterruptedException {
        System.out.println("正在查询，请稍后------");
        Thread.sleep(1000);
        System.out.println("学生的信息为:");
        System.out.println("  "+"姓名"+" "+"性别"+" "+"年龄"+" "+"学号"+" ");
        int i = 1;
        for (Student student1 : studentList) {
            System.out.println(i+"."+student1);
            i++;
        }
        System.out.println("正在返回主菜单-----");
        Thread.sleep(1000);
    }

}
